package com.test03;

import com.test03.utils.JdbcUtils_DBCP;

import java.sql.Connection;
import java.util.Date;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class TestDBCP {
    public static void main(String[] args) {
        Connection cn = null;
        PreparedStatement pstm = null;
        try {
            cn = JdbcUtils_DBCP.getConnection();
            System.out.println(cn);

            //区别
            //使用？ 占位符代替参数
            String sql = "INSERT INTO users(id,name,password,email,birthday) VALUES(?,?,?,?,?)";
            pstm = cn.prepareStatement(sql); //预编译sql，先写sql，不执行

            //手动给参数赋值
            pstm.setInt(1,5);  //根据参数顺序赋值,匹配？号
            pstm.setString(2,"zsh");
            pstm.setString(3,"123456");
            pstm.setString(4,"1720282908@qq.com");
            //主要sql.Date 和 util.Date 不同
            pstm.setDate(5,new java.sql.Date(new Date().getTime())); //获得时间戳

            //执行
            int i = pstm.executeUpdate();
            if(i > 0){
                System.out.println("插入成功");
            }

        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JdbcUtils_DBCP.releaseConnection(cn,pstm,null);
        }
    }

}
